package com.mxnavi.smartd.coreui.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavBackStackEntry;
import androidx.navigation.NavController;
import androidx.navigation.NavOptions;
import androidx.navigation.Navigation;


/**
 * @author ：zhong.jw
 * @date ：Created in 2024/2/4 9:47
 */
public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";

    private View mRootView;

    protected final <T extends View> T findViewById(@IdRes int viewId) {
        final View view = getView();
        if (view == null) {
            throw new IllegalStateException("findViewById invoke error");
        }
        return view.findViewById(viewId);
    }

    protected final void pop() {
        View view = getView();
        if (view == null) {
            throw new IllegalStateException("navigationTo invoke error");
        }
        Navigation.findNavController(view).popBackStack();
    }

    protected final void popTo(@IdRes int id, boolean inclusive) {
        View view = getView();
        if (view == null) {
            throw new IllegalStateException("navigationTo invoke error");
        }
        Navigation.findNavController(view).popBackStack(id, inclusive);
    }

    /**
     * single top类型的跳转。
     * 在当前页面则不响应，
     * 如果页面在栈中，弹出栈顶所有页面；
     * 否则，跳转指定页面
     * */
    protected final void navigationToSingleTop(@IdRes int id, Bundle args, NavOptions optionss) {
        View view = getView();
        if (view == null) {
            throw new IllegalStateException("navigationTo invoke error");
        }
        NavController navController = Navigation.findNavController(view);
        NavBackStackEntry navBackStackEntry = navController.getBackStack().peekLast();
        if (navBackStackEntry != null && navBackStackEntry.getDestination().getId() == id) {
            return;
        }
        if (!navController.popBackStack(id, false)) {
            navController.navigate(id, args, optionss);
        }
    }

    protected final void navigationTo(@IdRes int id) {
        navigationTo(id, null);
    }

    protected final void navigationTo(@IdRes int id, Bundle args) {
        navigationTo(id, args, null);
    }

    protected final void navigationTo(@IdRes int id, Bundle args, NavOptions options) {
        View view = getView();
        if (view == null) {
            throw new IllegalStateException("navigationTo invoke error");
        }
        if (options == null) {
            options = createDefaultNavOptions();
        }
        Navigation.findNavController(view).navigate(id, args, options);
    }

    protected NavOptions createDefaultNavOptions() {
        final NavOptions.Builder builder = new NavOptions.Builder();
        builder.setLaunchSingleTop(true);
        return builder.build();
    }

    @LayoutRes
    protected abstract int onInflateLayout();

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(onInflateLayout(), container, false);
        }
        return mRootView;
    }
}
